package com.wanmait.shoppingsys.controller.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oreilly.servlet.MultipartRequest;
import com.wanmait.shoppingsys.dao.UserinfoDAO;
import com.wanmait.shoppingsys.vo.UserInfo;

@WebServlet("/UserinfoController")
public class UserinfoController extends HttpServlet{
	
	private UserinfoDAO userinfoDAO =UserinfoDAO.getDAO();
	
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String action = request.getParameter("action");
		if(action==null) return;
		switch(action) 
		{
		case "reg":                     //注册
			this.reg(request, response);
			break;
		case "login":					//登录
			this.login(request, response);
			break;
		case "findpassword"	:         //找回密码
			this.findpassword(request, response);
			break;
		case "alter":				//修改密码
			this.alter(request, response);	
			break;
		case "logout":				//退出
			this.logout(request, response);	
			break;
		case "findinfo":				
			this.findinfo(request, response);	
			break;
		}
	}
	
	
	protected void findinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletContext().getContextPath();
		String userinfo    =  request.getParameter("userinfo");
		String info   = userinfoDAO.findinfo(userinfo);
		    if(info!=null)
		    {
		    	response.getWriter().write("false");
		    }
		    else{
		    	response.getWriter().write("true");
		    }

	}
	
	protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletContext().getContextPath();
		request.getSession().removeAttribute("userinfo");
		response.sendRedirect(path+"/");
	}
	
	protected void alter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = request.getServletContext().getContextPath();
		  String pass = request.getParameter("pass");
		  String idd = request.getParameter("id");
		  Integer  id =Integer.parseInt(idd);
		 
		  userinfoDAO.updatePass(id, pass);
		  
		  response.sendRedirect(path+"/userinfo/login.jsp?err=3");
		  
		
			
	}
	protected void findpassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = request.getServletContext().getContextPath();
		
		String username =request.getParameter("username");
		String email =request.getParameter("email");
		
		UserInfo userInfo = userinfoDAO.findemail(username,email);
			    
		if(userInfo==null)
		{
			
			response.sendRedirect(path+"/userinfo/findpass.jsp?err=1");
		}
		if(userInfo!=null)
		{ 
			int id = userInfo.getId();
			response.sendRedirect(path+"/userinfo/alter.jsp?id="+id);
		}
			
	}
	
	protected void reg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = request.getServletContext().getContextPath();
			String  name = request.getParameter("user_name");
			String pass = request.getParameter("pwd");
			String email   =  request.getParameter("email");
			
			UserInfo userInfo =new UserInfo();
			userInfo.setName(name);
			userInfo.setPass(pass);
			userInfo.setEmail(email);
			
			userinfoDAO.insert(userInfo);
			response.sendRedirect(path+"/userinfo/login.jsp");
			
	}
	
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = request.getServletContext().getContextPath();
		String username = request.getParameter("username");
		String pass = request.getParameter("pwd");
		UserInfo userinfo    = 	userinfoDAO.findByNameAndPass(username,pass);
		if(userinfo==null) {
			//登录失败
			response.sendRedirect(path+"/userinfo/login.jsp?err=1");
		}
		else {
			//登录成功了
			if(userinfo.isEnable()==false) {
				response.sendRedirect(path+"/userinfo/login.jsp?err=2");
			}else {
			HttpSession session = request.getSession();
			session.setAttribute("userinfo", userinfo);
			response.sendRedirect(path);
			}
			
		}
		
		
		
	}
	

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
